package hdu.edu.Controller;


import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import hdu.edu.Model.Person;

@Controller
@RequestMapping("/login")
public class LoginController {
	@Autowired
	private hdu.edu.Dao.personDao personDao;
	@RequestMapping(method=RequestMethod.GET)
	public ModelAndView get(){
		return new ModelAndView("login/login");
	}
	
	@RequestMapping(method=RequestMethod.POST)
	public ModelAndView post(HttpSession session,@RequestParam String username,
			@RequestParam String password){
	    
	      Person person= personDao.findByName(username);
	      if(person==null){
	    	ModelAndView mav1 =new ModelAndView("/login/login");
			mav1.addObject("error","用户名或密码错误!!!");
			return mav1;
	     }
		if (username.equals(person.getUserName()) && password.equals(person.getPassword())) {
			session.setAttribute("userName",username);
			
				return new ModelAndView("redirect:book");
	    }else{
		    ModelAndView mav =new ModelAndView("/login/login");
		    mav.addObject("error","用户名或密码错误!!!");
		    return mav;
		}
		
		
	
	}

	@RequestMapping(value="/add",method=RequestMethod.GET)
	public ModelAndView add(){
		ModelAndView mav=new ModelAndView("login/addPerson");
		return mav;
		
	}
	@RequestMapping(value="/add",method=RequestMethod.POST)
	public ModelAndView add(@RequestParam String username,
			@RequestParam String password){
		Person person=new Person();
		person.setUserName(username);
		person.setPassword(password);
		personDao.addPerson(person);
		ModelAndView mav=new ModelAndView("login/login");
		return mav;
		
	}


	
}
	

